﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Abs.App.Admin
{
    public partial class Edit_Room : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Role"] == null)
            {
                Response.Redirect("~/User/Unauthorized_Access.aspx");
            }

            if (((int)Session["Role"] != (int)Abs.Core.Models.User.RoleEnum.Admin))
            {
                Response.Redirect("~/User/Unauthorized_Access.aspx");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            var roomManagement = Abs.Core.Management.Manage.Room;
            int roomId = int.Parse(Request.QueryString["Id"]);
            if (!roomManagement.UpdateRoom(roomId, Text_Name.Text))
            {
                Response.Redirect("~/Default.aspx");
            }
            if(!roomManagement.DeleteRoomScheduleByRoomId(roomId))
            {
                Response.Redirect("~/Default.aspx");
            }
            for (int i = 0; i < Days.Items.Count; i++)
            {
                if (Days.Items[i].Selected)
                {
                    switch (Days.Items[i].Text)
                    {
                        case "Monday":
                            if (!roomManagement.CreateRoomSchedule(roomId, (int)DayOfWeek.Monday, Start_Time.Text, Last_Time.Text))
                            {
                                Response.Redirect("/Default.aspx");
                            }
                            break;

                        case "Tuesday":
                            if (!roomManagement.CreateRoomSchedule(roomId, (int)DayOfWeek.Tuesday, Start_Time.Text, Last_Time.Text))
                            {
                                Response.Redirect("/Default.aspx");
                            }
                            break;
                        case "Wednesday":
                            if (!roomManagement.CreateRoomSchedule(roomId, (int)DayOfWeek.Wednesday, Start_Time.Text, Last_Time.Text))
                            {
                                Response.Redirect("/Default.aspx");
                            }
                            break;
                        case "Thursday":
                            if (!roomManagement.CreateRoomSchedule(roomId, (int)DayOfWeek.Thursday, Start_Time.Text, Last_Time.Text))
                            {
                                Response.Redirect("/Default.aspx");
                            }
                            break;
                        case "Friday":
                            if (!roomManagement.CreateRoomSchedule(roomId, (int)DayOfWeek.Friday, Start_Time.Text, Last_Time.Text))
                            {
                                Response.Redirect("/Default.aspx");
                            }
                            break;
                        case "Saturday":
                            if (!roomManagement.CreateRoomSchedule(roomId, (int)DayOfWeek.Saturday, Start_Time.Text, Last_Time.Text))
                            {
                                Response.Redirect("/Default.aspx");
                            }
                            break;

                    }

                }
            }
            Response.Redirect("/Admin/Manage_Rooms.aspx");
            
        }
    }
}